package edu.uwosh.cs342.project3;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.widget.Toast;
import android.os.AsyncTask;

import edu.uwosh.cs342.project3.Login;

public class DBHelper {
	private HttpPost httppost;
	private StringBuffer buffer;
	private HttpResponse response;
	private HttpClient httpclient;
	private InputStream inputStream;
	
	private List<NameValuePair> nameValuePairs;
	private byte[] data;
	private String user, pass;
	public String StringOutput;
	public String returnString;
	
	//	Get quiz list
	public class getQuizzesTask extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... params) {
			try { // catches IOException below
				
				/*
				URL url = new URL("http://cs346.cs.uwosh.edu/students/godinc31/test.php");
				URLConnection con = url.openConnection();
				InputStream in = con.getInputStream();
				String encoding = con.getContentEncoding();
				encoding = encoding == null ? "UTF-8" : encoding;
				String body = IOUtils.toString(in, encoding);
				returnString = body;
				*/
				
				/*
				DefaultHttpClient client = new DefaultHttpClient();
			    JSONObject json = null;
			    String resoult = "";
			    try
			    {
			            HttpPost postRequest = new HttpPost("cs346.cs.uwosh.edu/students/godinc31/test.php");
			            HttpResponse postResponse = client.execute(postRequest);
			            HttpEntity postResponseEntity = postResponse.getEntity();
			            if (postResponseEntity != null) 
			                    resoult= EntityUtils.toString(postResponseEntity);
			            json = new JSONObject(resoult);             
			    }               
			    catch(Exception e)
			    {

			    }
				
				*/
				
				
				
				
				EasyHttpClient client = new EasyHttpClient();
				returnString = client.get("http://cs346.cs.uwosh.edu/students/godinc31/test.php");
				
				
				
				
				/*
				URL url = new URL("http://cs346.uwosh.edu/students/godinc31/test.php");
	               
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null){
                	returnString += inputLine;
                }
                */
                return returnString;
			}catch(Exception e){
				return e + " ";
			}
			
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			
		}
		
    }
	
	
	
	// Validate login
	public char validate(Login i){
		try {
			
			user = i.getUsername();
			pass = i.getUsername();

			httpclient = new DefaultHttpClient();
			httppost = new HttpPost("http://cs346.cs.uwosh.edu/students/godinc31/p3.php");
			

			nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("username",
					user.trim()));
			nameValuePairs.add(new BasicNameValuePair("password",
					pass.trim()));
			httppost.setEntity(new UrlEncodedFormEntity(
					nameValuePairs));

			response = httpclient.execute(httppost);
			inputStream = response.getEntity().getContent();

			data = new byte[256];

			buffer = new StringBuffer();
			int len = 0;
			while (-1 != (len = inputStream.read(data))) {
				buffer.append(new String(data, 0, len));
			}

			inputStream.close();
		}catch (Exception e) {
		
		}
		try{
		return buffer.charAt(0);
		}catch(NullPointerException e){
			return 'S';
		}
		
	
	}

	//	Get scores
	public String getScores(){
		return "";
	}
	
	//	Send score
	public String sendScore(){
		return "";
	}
}
